#input thread_id,and view its posts

import wsgiref.handlers

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from BeautifulSoup import BeautifulSoup
from google.appengine.ext import db
from xmw import tianya

from xmw import error
import os
import urllib2, urllib
import uuid
from xmw.db import ds
from google.appengine.ext import webapp
import __main__
from appengine_utilities import sessions
import logging
import xmw
from xmw.db import ds

class MainPage(xmw.RequestHandler):

    def get(self):
        self.init_session()          
        session_user = self.get_session_user()
        
        if session_user is None:
            login_action=ds.Login_Action()
            self.sess['login_action']   =login_action
                
            login_action.action ="/mine/my_tianya_post"
            login_action.params="q="
            login_action.created=xmw.get_cn_now_time()
            login_action.put()
            self.redirect('/login')
            return 
        
        sub_q=ds.Sub_Req.all()
        sub_q.filter('user_id = ',session_user.user_id)
        sub_q.filter('status = ',ds.Sub_Req_Status.IN_TRACE)
        #how to get all?
        lst_thread=[]
        lst_sub=sub_q.fetch(1000)
        
        #if IN_TRACE,should read the thread info,such as author,floor,etc.
        for sub in lst_sub:
            t_q=xmw.Thread.all()
            t_q.filter('thread_id = ', sub.thread_id)
            t=t_q.get()
            if t is not None:
                t.sub_req_id=sub.sub_req_id
                t.url=t.lst_page_url[0]
                #query sub count
                sub_q=ds.Sub_Req.all()
                sub_q.filter('status = ',ds.Sub_Req_Status.IN_TRACE)
                sub_q.filter('thread_id =',t.thread_id)
                cnt=sub_q.count()
                t.sub_cnt=cnt
                t.option=sub.option
                lst_thread.append(t)
        
        sub_q=ds.Sub_Req.all()
        sub_q.filter('user_id = ',session_user.user_id)
        sub_q.filter('status = ',ds.Sub_Req_Status.INIT)
        lst_sub=sub_q.fetch(1000)
        
        sub_q=ds.Sub_Req.all()
        sub_q.filter('user_id = ',session_user.user_id)
        sub_q.filter('status = ',ds.Sub_Req_Status.ACCEPTED)
        lst_sub2=sub_q.fetch(1000)
        lst_sub.extend(lst_sub2)
        for sub in lst_sub:
            t=xmw.Thread()
            t.title = sub.title
            t.url = sub.url
            t.sub_req_id=sub.sub_req_id
            t.status=sub.status
            t.chrAuthor="-"
            t.page_cnt =0
            t.option=sub.option
            #t.latest_post_date="-"
            #t.sub_cnt="-"
            lst_thread.append(t)
        
        count=len(lst_thread)
        logging.info("len(lst_thread);%d"%count)
        template_values ={
                            "session_user":session_user,
                            "lst_thread":lst_thread,
                            "count":count,
                        }
        
        self.render_template(template_values, "../html/my_tianya_post.html")
        return
    
    

def main():
    application = webapp.WSGIApplication(
                                       [('/mine/my_tianya_post', MainPage)],
                                       debug=True)
    wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
  main()
